﻿using System;
using System.Windows;
using Microsoft.Practices.Unity;
using WpfApplication1.TaskModule;
using WpfApplication1.Wpf;
using WpfApplication1.ContactModule;

namespace WpfApplication1
{
    /// <summary>
    ///     Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application, IViewModelResolver
    {
        private readonly IUnityContainer _container = new UnityContainer();

        public T Resolve<T>() where T : ViewModel
        {
            return _container.Resolve<T>();
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            _container.RegisterInstance<IViewModelResolver>(this);

            {
                Type type = Type.GetType("WpfApplication1.ItemsDbContext,WpfApplication1Model");
                _container.RegisterType(typeof (ITaskItemsRepository), type, new ContainerControlledLifetimeManager());
            }
            {
                Type type = Type.GetType("WpfApplication1.ItemsDbContext,WpfApplication1Model");
                _container.RegisterType(typeof(IContactItemsRepository), type, new ContainerControlledLifetimeManager());
            }

            _container.RegisterType<TasksViewModel, TasksViewModel>(new ContainerControlledLifetimeManager());
            _container.RegisterType<ContactsViewModel, ContactsViewModel>(new ContainerControlledLifetimeManager());

            var mainWindow = new MainWindow(_container.Resolve<MainWindowViewModel>());

            mainWindow.Show();
        }
    }
}